import {Module} from "vuex";

import {
  getvwComplaintDataInLast7Days,
  getvwNCVQSDataInLast7Days,
  getaudiComplaintDataInLast7Days,
  getaudiNCVQSDataInLast7Days,
  getTicktesTrendBySourceId
} from "@/service/main/analysis/dashboard";

import {IDashboardState} from "./types";
import {IRootState} from "../../types";
import localCache from '@/utils/cache'

const dashboardModule: Module<IDashboardState, IRootState> = {
  namespaced: true,
  state() {
    return {
      vwComplaintDataInLast7Days: [],
      vwNCVQSDataInLast7Days: [],
      audiComplaintDataInLast7Days: [],
      audiNCVQSDataInLast7Days: [],
      ticketsTrendDataBysourceId: []
    };
  },
  mutations: {
    changevwComplaintCategory(state, list) {
      state.vwComplaintDataInLast7Days = list;
    },
    changevwNCVQSCategory(state, list) {
      state.vwNCVQSDataInLast7Days = list;
    },
    changeaudiComplaintCategory(state, list) {
      state.audiComplaintDataInLast7Days = list;
    },
    changeaudiNCVQSCategory(state, list) {
      state.audiNCVQSDataInLast7Days = list;
    },
    changeTicketsTrendBySourceId(state, list) {
      state.ticketsTrendDataBysourceId = list
    }

  },
  actions: {
    async getDashboardDataAction({commit}) {
      const vwComplaintDataInLast7Days = await getvwComplaintDataInLast7Days();
      commit("changevwComplaintCategory", vwComplaintDataInLast7Days.data);
      const vwNCVQSDataInLast7Days = await getvwNCVQSDataInLast7Days();
      commit("changevwNCVQSCategory", vwNCVQSDataInLast7Days.data);
      const audiComplaintDataInLast7Days = await getaudiComplaintDataInLast7Days();
      commit("changeaudiComplaintCategory", audiComplaintDataInLast7Days.data);
      const audiNCVQSDataInLast7Days = await getaudiNCVQSDataInLast7Days();
      commit("changeaudiNCVQSCategory", audiNCVQSDataInLast7Days.data);

      // 默认获取VW的爬取的数据
      const ticketsTrendBySourceId = await getTicktesTrendBySourceId('VW');
      commit("changeTicketsTrendBySourceId", ticketsTrendBySourceId.data);
    },

    async getDashboardDataActionBySourceId({commit}) {
      const sourceId = localCache.getCache('sourceId-line-chart') ?? 'complaint_12365auto'
      const ticketsTrendBySourceId = await getTicktesTrendBySourceId(sourceId);
      commit("changeTicketsTrendBySourceId", ticketsTrendBySourceId.data);
    }
  }
};


export default dashboardModule;
